import {observable,action} from "mobx";

class breadcrumbStore {
    @observable breadcrumbList=[
        {
            path:'/homepage/indexpage',
            name:'首页',
        },
        // {
        //     path:sessionStorage.getItem('routerpath')||undefined,
        //     name:sessionStorage.getItem('routername')||undefined
        // }
    ]
    @action
    changebreadcrumb=(path,name)=>{
        let obj={}
        obj.path=path
        obj.name=name
        sessionStorage.setItem('routerpath',path)
        sessionStorage.setItem('routername',name)
        if(obj.path!==this.breadcrumbList[0].path){
            /*点击的不是主页，第一种情况：就往数组里面加一个对象   第二种：（将第二个对象替换成新的path和name）*/
            if(this.breadcrumbList[1]===undefined){
                this.breadcrumbList.push(obj)
            }else {
                this.breadcrumbList[1].path=""
                this.breadcrumbList[1].name=""
                // this.breadcrumbList[1].path=obj.path
                // this.breadcrumbList[1].name=obj.name
                this.breadcrumbList[1].path=sessionStorage.getItem('routerpath')||undefined
                this.breadcrumbList[1].name=sessionStorage.getItem('routername')||undefined
            }
        }else {
            /*点击的是主页，删除其他对象，只保留第一个对象。*/
            if(this.breadcrumbList[1]!==undefined){
                this.breadcrumbList.splice(1,1)
            }else if(this.breadcrumbList[0].path===sessionStorage.getItem('routerpath')||this.breadcrumbList[0].name===sessionStorage.getItem('routername')){
                this.breadcrumbList.splice(1,1)
            }
        }
    }
}
export default breadcrumbStore